package wellness.activities;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;

import wellness.activities.adapters.AtividadesAdapter;
import wellness.activities.adapters.SemanasAdapter;
import wellness.activities.alert.Agendador;
import wellness.activities.beans.AgendamentoBean;
import wellness.activities.beans.AtividadeBean;
import wellness.activities.beans.LoadBeans;
import wellness.activities.data.AgendamentoData;
import wellness.activities.util.Dialogs;
import wellness.activities.util.Utilidades;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

public class NewSchedule extends Activity {

	Context context;
	Spinner atividades;
	Button dia;
	Button hora;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_schedule);
		context = this;

		ImageView goBack = (ImageView) findViewById(R.id.imageViewGoBack);
		goBack.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent intent = new Intent();
				setResult(RESULT_OK, intent);
				finish();
			}
		});

		ImageView goHome = (ImageView) findViewById(R.id.imageViewGoHome);
		goHome.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent intent = new Intent(NewSchedule.this, Main.class);
				startActivity(intent);
			}
		});

		ImageView adicionar = (ImageView) findViewById(R.id.imageViewSaveNovoAgendamento);
		adicionar.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				adicionarAgendamento();
			}
		});

		atividades = (Spinner) findViewById(R.id.spinnerAtividades);
		AtividadesAdapter s = new AtividadesAdapter(this);
		atividades.setAdapter(s);

		GregorianCalendar dataAtual = Agenda.dateSelected;

		int cDia = dataAtual.get(Calendar.DATE);
		int cMes = dataAtual.get(Calendar.MONTH);
		int cAno = dataAtual.get(Calendar.YEAR);

		int cHora = new GregorianCalendar().get(Calendar.HOUR_OF_DAY);
		int cMinuto = new GregorianCalendar().get(Calendar.MINUTE);

		dia = (Button) findViewById(R.id.ButtonDia);
		dia.setText(Utilidades.getNumeroFormatado(cDia) + "/"
				+ Utilidades.getNumeroFormatado(cMes + 1) + "/" + cAno);
		dia.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Dialogs.showDialogDate(v);
			}
		});

		hora = (Button) findViewById(R.id.buttonHora);
		hora.setText(Utilidades.getNumeroFormatado(cHora) + ":"
				+ Utilidades.getNumeroFormatado(cMinuto));
		hora.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Dialogs.showDialogTime(v);
			}
		});
		
	}

	public void adicionarAgendamento() {
		AtividadeBean a = LoadBeans.getAtividadePorId(Integer.valueOf(String
				.valueOf(atividades.getSelectedItemId())));
		String[] datas = dia.getText().toString().split("/");
		String[] horarios = hora.getText().toString().split(":");
		// GregorianCalendar dataAtual = new GregorianCalendar();
		GregorianCalendar dataCalendar = new GregorianCalendar(
				Integer.parseInt(datas[2]), Integer.parseInt(datas[1]) - 1,
				Integer.parseInt(datas[0]), Integer.parseInt(horarios[0]),
				Integer.parseInt(horarios[1]));

		AgendamentoBean agendamento = new AgendamentoBean(a, dataCalendar);

		// if(dataAtual.after(dataCalendar)){
		AgendamentoData data = new AgendamentoData(context);
		data.insert(agendamento);

		Intent intent = new Intent();
		setResult(RESULT_OK, intent);
		finish();
	}
}
